local skill = fk.CreateSkill {
  name = "#ling__wooden_ox_skill",
  attached_equip = "ling__wooden_ox",
}

Fk:loadTranslationTable{
  ["#ling__wooden_ox_skill"] = "木牛流马",
  ["#ling__wooden_ox-give"] = "木牛流马：你可以将至多两张弃牌分配给任意角色",
}

skill:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skill.name) and player.phase == Player.Discard then
      local cards = {}
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from == player and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
                table.contains(player.room.discard_pile, info.cardId) then
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end
      end, Player.HistoryPhase)
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    local result = room:askToYiji(player, {
      cards = cards,
      targets = room.alive_players,
      skill_name = skill.name,
      min_num = 0,
      max_num = 2,
      prompt = "#ling__wooden_ox-give",
      expand_pile = cards,
      skip = true,
    })
    for _, ids in pairs(result) do
      if #ids > 0 then
        event:setCostData(self, {extra_data = result})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:doYiji(event:getCostData(self).extra_data, player, skill.name)
  end,
})

return skill
